<?php /** @noinspection ALL */

//统计查询

namespace app\admin\controller;


use think\facade\Db;

class AddUp {

	function formneed(){
		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		if($type==2){//代理
			$agentlist = Db::query("select id,level,nick_name from agent where find_in_set('{$id}',team_tag) ");

		}else{
			$agentlist = Db::table('agent')
				->field('id,level,nick_name')
				->where('level', '<=', 3)
				->select()
				->all();
		}

		return json(['code' => 1, 'data' => $agentlist]);
	}

	function show($page=1,$pageSize=10){
		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		$condition = ' and 1=1';

		if($type==2){//代理
			if (!empty($input['time'])) {
				$start=$input['time'][0]/1000;
				$end=$input['time'][1]/1000;
				$condition .= " and u.add_time >= $start and u.add_time <=  $end";
			}

			if(!empty($input['id'])){
				$condition .= " and find_in_set('{$input['id']}',agent.team_tag) ";
			}else{
				$condition .= " and find_in_set('{$id}',agent.team_tag) ";
			}

			$list = Db::query("select agent.nick_name as agent_nick_name,agent.real_name as agent_real_name,agent.phone as agent_phone,u.nick_name,u.real_name,u.tel,agent.ratio,agent.rebate,user_account.amount,u.id,user_account.gold,user_account.frozen_amount,user_account.frozen_gold,user_account.total_recharge,user_account.total_withdrawal,user_account.total_transaction,user_account.total_fee from agent join system_user as u on u.agent_id=agent.id join user_account on user_account.uid=u.id where 1".$condition." limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num=Db::query("select count(u.id) as sum from agent join system_user as u on u.agent_id=agent.id join user_account on user_account.uid=u.id where 1".$condition);

			$total_num=$total_num[0]['sum'];

		}else{
			if (!empty($input['id'])) {
				$input['id']=intval($input['id']);
				$condition .= " and find_in_set('{$input['id']}',agent.team_tag) ";
			}

			if (!empty($input['time'])) {
				$start=$input['time'][0]/1000;
				$end=$input['time'][1]/1000;
				$condition .= " and u.add_time >= $start and u.add_time <=  $end";
			}

			if(!empty($input['time'])||!empty($input['id'])){
				$list = Db::query("select agent.nick_name as agent_nick_name,agent.real_name as agent_real_name,agent.phone as agent_phone,u.nick_name,u.real_name,u.tel,agent.ratio,agent.rebate,user_account.amount,u.id,user_account.gold,user_account.frozen_amount,user_account.frozen_gold,user_account.total_recharge,user_account.total_withdrawal,user_account.total_transaction,user_account.total_fee from agent join system_user as u on u.agent_id=agent.id join user_account on user_account.uid=u.id where 1".$condition." limit ".(($page-1)*$pageSize).",$pageSize");

				$total_num=Db::query("select count(u.id) as sum from agent join system_user as u on u.agent_id=agent.id join user_account on user_account.uid=u.id where 1".$condition);

				$total_num=$total_num[0]['sum'];
			}else{
				$list=[];
				$total_num=0;
			}

		}

		if(!empty($list)){
			for ($i=0;$i<count($list);$i++){
				$list[$i]['commission_a']=($list[$i]['total_transaction']+$list[$i]['total_fee'])*$list[$i]['ratio']/100;
				$list[$i]['commission_b']=($list[$i]['total_transaction']+$list[$i]['total_fee'])*$list[$i]['rebate']/100;
			}
		}

		return json(['code' => 1, 'data' => $list,'total_num'=>$total_num]);
	}

}